package com.znf4.controller.good;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.znf4.base.BaseReturnModel;
import com.znf4.constants.CommonConst;
import com.znf4.enums.OperationStatusEnum;
import com.znf4.good.imgUrl.entity.ImgUrl;
import com.znf4.good.imgUrl.server.ImgUrlServer;
import com.znf4.id.IdGenerator;
import com.znf4.utils.OSSFile;

/**
 * 商品多张图片
 */
@Controller
@RequestMapping("/good/imgUrl")
public class ImgUrlController {
	
	
	private static final Logger LOG = LoggerFactory.getLogger(ImgUrlController.class);
	
	@Autowired
	private ImgUrlServer imgUrlServer ;
	
	@Autowired
	private IdGenerator idGenerator ;
	
   
	
	/**
	 * 函数功能说明 ： 添加商品图片
	 *
	 * @参数： @return @return String @throws
	 * @param imgUrl 图片基本信息
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> add(ImgUrl imgUrl , String goodType , @RequestParam(value = "file", required = false)List<MultipartFile> files) {

		try {
			long startTime = System.currentTimeMillis();
			// 文件上传OSS
			for (MultipartFile multipartFile : files) {
				imgUrl.setId(idGenerator.getGoodNameNumber());
				String fileName;
				if (goodType != null && goodType.equals("crops")) { // 农作物
					fileName = CommonConst.CROPS_GOOD_IMG_NUMBER + imgUrl.getId();
				} else if (goodType != null && goodType.equals("free")) { // 免费商品
					fileName = CommonConst.FREE_GOOD_IMG_NUMBER + imgUrl.getId();
				} else if(goodType != null && goodType.equals("jifen")){ // 积分商品
					fileName = CommonConst.JIFEN_GOOD_IMG_NUMBER + imgUrl.getId();
				}else{
					return BaseReturnModel.getMapModel("faild", "", "商品类型不存在");
				}
				
				String orignalFileName = multipartFile.getOriginalFilename();
				if (multipartFile != null && orignalFileName != null && orignalFileName.length() > 0) {
					imgUrl.setImg_url(OSSFile.upFile(multipartFile, fileName));
					LOG.info("商品幅图上传图片到oss.....并设置数据库参数。");
				}
				
				imgUrlServer.addGoodImgUrl(imgUrl);
				
			}
			long endTime = System.currentTimeMillis();
			System.out.println("运行时间：" + String.valueOf(endTime - startTime) + "ms");
			
		} catch (Exception e) {
			LOG.info("添加其他图片异常 :" , e.getMessage());
			return BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc());
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "");
	}

	
	
	/**
	 * 函数功能说明 ： 修改商品图片
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> update(List<ImgUrl> imgUrls , @RequestParam(value = "file", required = false)List<MultipartFile> files) {
		try {
			for(int i = 0 ; i < imgUrls.size() ; i ++  ){
				//获取实体
				ImgUrl imgUrl = imgUrls.get(i) ;
				
				MultipartFile multipartFile = files.get(i) ;
				OSSFile.noChangeUpFile(multipartFile, imgUrl.getImg_url()) ;
			}
		} catch (Exception e) {
			LOG.info("修改其他图片异常 :" , e.getMessage());
			return BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc());
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "");
	}
	

	/**
	 * 函数功能说明 ： 删除农作物商品
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/delOne", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> delOne(ImgUrl imgUrl ) {
		
		try {
			imgUrlServer.delOneGoodImgUrl(imgUrl);
			// 删除文件 OSS
			OSSFile.deleteFile(imgUrl.getImg_url());

		} catch (Exception e) {
			LOG.info("删除单个其他图片异常 :", e.getMessage());
			return BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc());
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "");
	}

	/**
	 * 函数功能说明 ： 删除多条农作物商品
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/delAll", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> delAll(String[] ids) {

		try {
			List<ImgUrl> list = new ArrayList<>();
			for (int i = 0; i < ids.length; i++) {

				ImgUrl imgUrl = new ImgUrl();
				imgUrl.setId(ids[i]);
				list.add(imgUrl);
				// 删除文件
				ImgUrl delImg = imgUrlServer.queryOne(imgUrl);
				OSSFile.deleteFile(delImg.getImg_url());
			}
			imgUrlServer.delAllGoodImgUrl(list);
		} catch (Exception e) {
			LOG.info("删除多个其他商品图片 ：" , e.getMessage() );
			return BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc());
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "");
	}
	
	
	/**
	 * 函数功能说明 ： 获取商品所有图片
	 *
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/queryAll", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> queryAll(ImgUrl imgUrl ) {
		
		List<ImgUrl> list = imgUrlServer.queryAll(imgUrl);
		return BaseReturnModel.getMapModel("success", list , "");
	}
	

	

}
